package ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import lombok.Getter;
import lombok.Setter;
import ssh.entity.User;
import ssh.service.UserService;

import java.util.List;

public class UserAction extends ActionSupport implements ModelDriven<User>, Preparable {


    @Setter
    private UserService userService;

    @Setter
    private Integer id;
    @Getter
    private User model;

    @Getter
    private List<User> userList;


    public String viewCreate() {
        return SUCCESS;
    }

    public String viewList() {
        userList = userService.list();
        return SUCCESS;
    }

    public String viewUpdate() {
        model = userService.getById(id);
        System.out.println(model);
        return SUCCESS;
    }

    public void prepareCreate() {
        model = new User();
    }

    public String create() {
        userService.save(model);
        return SUCCESS;
    }


    public String delete() {
        userService.deleteById(id);
        return SUCCESS;
    }

    public void prepareUpdate() {
        model = new User();
    }

    public String update() {
        userService.update(model);
        return SUCCESS;
    }


    @Override
    public void prepare() throws Exception {

    }
}
